<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
<script src="/res/third/laytpl/template.js"></script>
<script src="/res/common/js/jquery.form.js"></script>
<script src="/res/third/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/res/third/easyUI/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
	<script type="text/javascript">
        function sosuo(){
            var lpath =location.pathname;
            var pageNo = 1;
            $("#soform").attr("action",lpath+"?pageNo="+pageNo);
            $("#soform").submit();
        }
        
        function deleteRow(id, value){
            var message = (value == 0 ? "确认恢复？": "确认关闭？");
            art.dialog.confirm(message,function(){
                art.dialog.post({
                    url:"/level/rule/delete.do", data:{id: id, value: value}, success:function(data){
                        if(data.success){
                            window.location.reload();
                        }
                    }

                })
            },function(){

            })
        }

        function editRow(id) {
            $.getJSON('/level/rule/get.do', { id: id }, function (data, textStatus, jqXHR) {
                if (data.success) {
                    addRow(data.obj);
                }
            });
        }
        function addRow(data) {
            if (!data) { data = {}; }
            dialog = art.dialog({
                title: "新增规则",
                lock: true,
                content: template("edit_template", data),
                okVal: "提交",
                ok: function () {
                    // 增加验证
					var ruleName = $("input[name='ruleName']").val();
					if ($.trim(ruleName).length == 0) {
						alert("请填写规则名称");
						return false;
					}
					var ruleCode = $("input[name='ruleCode']").val();
                    if ($.trim(ruleCode).length == 0) {
                        alert("请填写规则编码");
                        return false;
                    }
                    var amount = $("input[name='amount']").val();
                    if ($.trim(amount).length == 0 || !$.isNumeric(amount)) {
                        alert("请填写正确的数量");
                        return false;
                    }
                    var subData = $("#edit_form").serialize();
                    art.dialog.post({ url:"/level/rule/save.do", data: subData, success:function(data){
                        if(data.success){
                            window.location.reload();
                        }
                    }
                    });
                }
            });
        }
        template.helper('getJsonString', function(limitRule, field) {
            console.log(limitRule);
            if (!limitRule) { return; }
            if (typeof(limitRule) == "string") {
                var jsonObj = $.parseJSON(limitRule);
                return jsonObj[field];
            }
            return limitRule[field];
        });
	</script>
	<script type="text/html" id="edit_template">
		<form id="edit_form" action="/level/config/save.do" method="post" class="form-horizontal" role="form">
			<input type="hidden" name="id" value="{{id}}">
			<div class="form-group">
				<label  class="col-sm-4 control-label">规则名称</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="请输入规则名称" name="ruleName" value="{{ruleName}}">
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-4 control-label">规则编码</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="请输入规则编码" name="ruleCode" value="{{ruleCode}}">
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-4 control-label">类型</label>
				<div class="col-sm-8">
                    <input type="radio" name="type" value="1" {{if !type}}checked{{else if type == 1}}checked{{/if}}>增加
                    <input type="radio" name="type" value="-1" {{if type && (type == -1)}}checked{{/if}}>减少
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-4 control-label">数量</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="请输入数量" name="amount" value="{{amount}}">
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-4 control-label">单位</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="单位(前台显示)" name="unit" value="{{unit}}">
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-4 control-label">限制规则</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="最多可获取多少次" name="maxCount" value="{{ limitRule | getJsonString: "max_count"}}">
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-4 control-label"></label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="每日获取最大数量" name="maxAmountADay" value="{{ limitRule | getJsonString: "max_amount_a_day" }}">
				</div>
			</div>
		</form>
	</script>
</head>
<body>
	<div class="panel panel-info panel-content">
		<div class="panel-heading">
			<div class="row">
			  <div class="col-md-9">
				  <form class="form-inline" method="post">
					  <div class="form-group">
						  <label for="ruleName">规则名称</label>
                          <input type="text" class="form-control" id="ruleName" name="ruleName" value="${ruleName!}" placeholder="规则名称">
						  <select class="form-control" name="removed">
							  <option value="0" [#if (removed!0) == 0]selected[/#if]>有效规则</option>
							  <option value="1" [#if removed?? && removed == 1]selected[/#if]>无效规则</option>
						  </select>
					  </div>
					  <button type="submit" class="btn btn-default btn-primary" onclick="sosuo();">搜索</button>
				  </form>
			  </div>
			  <div class="col-md-3">
				<button style="float: right" type="button" onclick="addRow()" class="btn btn-info"> <span class="glyphicon glyphicon-plus"></span> &nbsp;新增规则</button>
			  </div>
			</div>
		</div>
		<table class="table table-hover table-bordered table-striped table-condensed">
			<tr>
				<th>#</th>
				<th>规则名称</th>
				<th>规则标识</th>
				<th>类型</th>
				<th>数量</th>
				<th>限制规则</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
			[#list pagination.list as item]
			<tr>
				<td align="center">${pagination.pageSize * (pagination.pageNo-1)+item_index+1}</td>
				<td align="center">${item.ruleName!}</td>
				<td align="center">${item.ruleCode!}</td>
				<td align="center">${item.type!}</td>
				<td align="center">${item.amount!}${item.unit!}</td>
				<td align="center">
                    [#if item.limitRule??]
                        [#assign r = item.limitRule?eval/]
                        [#if r.max_amount_a_day??]最多可获取 ${r.max_amount_a_day!} 成长值[/#if]
                        [#if r.max_count??]最多可获取 ${r.max_count!} 次[/#if]
                    [/#if]
                </td>
				<td align="center">${item.createtime?string("yyyy-MM-dd HH:mm:ss")!}</td>
				<td align="center">
					<button type="button" onclick="editRow('${item.id}');" class="btn btn-info btn-xs">编辑</button>
					[#if item.removed == 0]
					<button type="button" onclick="deleteRow('${item.id}', 1);" class="btn btn-info btn-xs btn-danger">关闭</button>
					[#else]
					<button type="button" onclick="deleteRow('${item.id}', 0);" class="btn btn-info btn-xs btn-danger">恢复</button>
					[/#if]
				</td>
			</tr>
			[/#list]
		</table>
		<div class="panel-footer">[@fenye p = pagination form = 'infoForm' /]</div>
	</div>
</body>
</html>
